package com.iot08.api.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 1/17/22 3:54 PM
 */
public class CommonUtil {

    /**
     * 对json数组排序，
     * @param jsonArr
     * @param sortKey 排序关键字
     * @param is_desc is_desc-false升序列  is_desc-true降序 (排序字段为字符串)
     * @return
     */
    public static JSONArray jsonArraySort(JSONArray jsonArr, String sortKey, boolean is_desc) {
        //存放排序结果json数组
        JSONArray sortedJsonArray = new JSONArray();
        //用于排序的list
        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        //将参数json数组每一项取出，放入list
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.getJSONObject(i));
        }
        //快速排序，重写compare方法，完成按指定字段比较，完成排序
        Collections.sort(jsonValues, new Comparator<JSONObject>() {
            //排序字段
            private  final String KEY_NAME = sortKey;
            //重写compare方法
            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valA = new String();
                String valB = new String();
                try {
                    valA = a.getString(KEY_NAME);
                    valB = b.getString(KEY_NAME);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //是升序还是降序
                if (is_desc){
                    return -valA.compareTo(valB);
                } else {
                    return -valB.compareTo(valA);
                }

            }
        });
        //将排序后结果放入结果jsonArray
        for (int i = 0; i < jsonArr.size(); i++) {
            sortedJsonArray.add(jsonValues.get(i));
        }
        return sortedJsonArray;
    }

    public static int compareVersions(String version1, String version2) {
        String[] parts1 = version1.split("\\.");
        String[] parts2 = version2.split("\\.");

        int length = Math.max(parts1.length, parts2.length);

        for (int i = 0; i < length; i++) {
            int num1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
            int num2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;

            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            }
        }

        return 0;
    }
    
    public static void main(String[] args){
        JSONObject item = new JSONObject();
        System.out.println(String.format("%s", item.getIntValue("canVideo")));
        /*

        */
        String jsonStr = "[{\"groupId\":\"123456789000008\",\"groupType\":2,\"groupName\":\"宝贝08\",\"groupImage\":\"xXXX\",\"canVideo\":1},{\"groupId\":\"123456789000009\",\"groupType\":2,\"groupName\":\"常丽\",\"groupImage\":\"xXXX\",\"canVideo\":1},{\"groupId\":\"123456789000010\",\"groupType\":2,\"groupName\":\"安妮\",\"groupImage\":\"xXXX\",\"canVideo\":1}]";

        JSONArray friendsArr = JSONArray.parseArray(jsonStr);
        System.out.println(String.format("friendsArr 排序前:\n%s", friendsArr.toJSONString()));
        friendsArr = CommonUtil.jsonArraySort(friendsArr,"groupName", false);
        System.out.println(String.format("friendsArr 排序后:\n%s", friendsArr.toJSONString()));


    }

}
